
package org.openeai.lite.console.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;
import org.openeai.lite.common.control.BrokerController;
import org.openeai.lite.common.control.ConfigController;
import org.openeai.lite.common.control.ESBController;
import org.openeai.lite.common.control.RouterController;
import org.openeai.lite.common.util.BrokerShutdownHook;
import org.openeai.lite.common.util.Constants;
import org.openeai.lite.common.util.DateTimeUtils;
import org.openeai.lite.common.util.ESBShutdownHook;
import org.openeai.lite.common.util.RouterShutdownHook;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


public class StartupServlet extends HttpServlet
{

    private static Logger log = Logger.getLogger(StartupServlet.class);

    private static final long serialVersionUID = 1L;
    
    ApplicationContext applicationContext = null;
    
    @Override
    public void init() throws ServletException
    {
        loadShutdownHooks();
        loadControllers();
        loadApplicationContext();
        startComponents();
        showStatus();
    }


    private void loadShutdownHooks() {
        log.info("Adding shutdown hooks...");
        Runtime.getRuntime().addShutdownHook(RouterShutdownHook.getInstance());
        Runtime.getRuntime().addShutdownHook(ESBShutdownHook.getInstance());
        Runtime.getRuntime().addShutdownHook(BrokerShutdownHook.getInstance());        
    }


    private void loadControllers() {
        try
        {
            log.info("Loading controller classes...");            
            ConfigController.getInstance();           
            BrokerController.getInstance();
            RouterController.getInstance();
            ESBController.getInstance();
        }
        catch (Exception e)
        {
            log.error(e,e);
        }
    }

    private void loadApplicationContext() {
        log.info("loading application context.");
        try {
            if (applicationContext == null) {
                applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
                ConfigController.getInstance().setApplicationContext(applicationContext);
            }
        }
        catch (Exception e) {
            log.error(e,e);
            //kaboom!
            System.exit(1);
        }
    }

    private void startComponents() {
        try {
            if (Constants.AUTOSTART == "true") {
                log.info("Starting main components with default configuration");
                BrokerController.getInstance().start(null, null);
                RouterController.getInstance().start(null, null);
                //ESBController.getInstance().start(null, null);
            }
            else {
                log.info("Skipping autostart...");
            }
        }
        catch (Exception e)
        {
            log.error(e,e);
        }
    }


    private void showStatus() {
        try
        {
           log.info("\n" +
                    "======================================================================\n" +
                    "OPENEAI LITE - Ready for business\n" +
                    DateTimeUtils.now() + "\n" +
                    "----------------------------------------------------------------------\n" +
                    "\n" +
                    "Component status\n" +
                    "- Message Broker         : " + BrokerController.getInstance().isStarted() + "\n" +
                    "- Message Router         : " + RouterController.getInstance().isStarted() + "\n" +
                    "- Enterprise Service Bus : " + ESBController.getInstance().isStarted() + "\n" +
                    "\n" +
                    "======================================================================");
        }
        catch (Exception e)
        {
            log.error(e,e);
        }
    }

}
